package com.stormrage.edu.serv.edu.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.edu.dao.edu.EduVideoDao;
import com.stormrage.edu.serv.edu.EduVideoServ;
import com.stormrage.edu.serv.learn.LeaStudentVideoServ;
import com.stormrage.entity.edu.EduVideo;
import com.stormrage.entity.edu.rep.EduVideoRep;
import com.stormrage.entity.learn.req.LeaStudentVideoReq;
import com.stormrage.entity.learn.view.LeaStudentVideo;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.NumberUtil;

/**
 * @Title: EduVideoServImpl.java
 * @Package com.stormrage.edu.serv.edu.impl
 * @Description: 课件 Service
 * @author www.stormragetech.com
 * @date 2016年4月8日
 * @version V1.0
 */
@Service
public class EduVideoServImpl implements EduVideoServ {

	private static final Logger logger = LoggerFactory.getLogger(EduVideoServImpl.class);

	@Autowired
	private EduVideoDao eduVideoDao;

	@Autowired
	private LeaStudentVideoServ leaStudentVideoServ;

	@Override
	public EduVideo queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduVideoServ 】- 【 QueryById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduVideo entity = eduVideoDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EduVideoServ 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public List<EduVideo> queryListById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduVideoServ 】- 【 QueryListByForExcept 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduVideo> array = eduVideoDao.queryListById(map);

		logger.debug(" 【 Serv 】- 【 EduVideoServ 】- 【 QueryListByForExcept 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduVideo> queryListByMajorId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduVideoServ 】- 【 QueryListByMajorId 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduVideo> array = eduVideoDao.queryListByMajorId(map);

		logger.debug(" 【 Serv 】- 【 EduVideoServ 】- 【 QueryListByMajorId 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduVideoRep> queryRepListByCourseId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduVideoServ 】- 【 queryRepListByCourseId 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduVideoRep> array = eduVideoDao.queryRepListByCourseId(map);
		int seq = 1;

		Iterator<EduVideoRep> it = array.iterator();
		while (it.hasNext()) {
			it.next().setSeq(seq);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EduVideoServ 】- 【 queryRepListByCourseId 】- 完成运行 ");
		return array;

	}

	@Override
	public List<EduVideoRep> queryRepListByChapId(final LeaStudentVideoReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduVideoServ 】- 【 queryRepListByChapId 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("stuId", arg0.getStuId());
		map.put("chapId", arg0.getChapId());

		List<EduVideoRep> array = eduVideoDao.queryRepListByChapId(map);
		List<EduVideo> arr = eduVideoDao.queryListByChapId(map);

		if (!Integer.valueOf(array.size()).equals(arr.size())) {

			Iterator<EduVideo> it = arr.iterator();
			while (it.hasNext()) {
				EduVideo video = it.next();
				for (EduVideoRep videoRep : array) {
					if (video.getId().equals(videoRep.getId())) {
						it.remove();
					}
				}
			}

			for (EduVideo video : arr) {
				LeaStudentVideo entity = new LeaStudentVideo(arg0.getStuId(), video.getId(), 0);
				leaStudentVideoServ.create(entity);
			}
		}

		array = eduVideoDao.queryRepListByChapId(map);
		int seq = 1;

		Iterator<EduVideoRep> it = array.iterator();
		while (it.hasNext()) {
			EduVideoRep entity = it.next();
			String progress = null == entity.getLen() ? "0%"
					: NumberUtil.initPerc(Integer.valueOf(entity.getLen()), Integer.valueOf(entity.getVideoLen()), "%");
			Integer right = seq % 3 == 0 ? 0 : 1;
			entity = EduVideoRep.instance(entity, seq, progress, right);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EduVideoServ 】- 【 queryRepListByChapId 】- 完成运行 ");
		return array;

	}

}
